<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "sys_role".
 *
 * @property int $roleid
 * @property string $role_code
 * @property string $role_name
 * @property string|null $role_desc
 * @property string $role_type
 * @property string $role_status
 * @property int $role_index
 * @property int $is_default
 * @property int $is_delete
 * @property string $create_at
 * @property string $update_at
 */
class SysRole extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'sys_role';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['role_code', 'role_name'], 'required'],
            [['role_index', 'is_default', 'is_delete'], 'integer'],
            [['create_at', 'update_at'], 'safe'],
            [['role_code', 'role_name', 'role_type', 'role_status'], 'string', 'max' => 50],
            [['role_desc'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'roleid' => 'Roleid',
            'role_code' => 'Role Code',
            'role_name' => 'Role Name',
            'role_desc' => 'Role Desc',
            'role_type' => 'Role Type',
            'role_status' => 'Role Status',
            'role_index' => 'Role Index',
            'is_default' => 'Is Default',
            'is_delete' => 'Is Delete',
            'create_at' => 'Create At',
            'update_at' => 'Update At',
        ];
    }

    /**
     * @param $userid
     * @return array|\yii\db\DataReader
     * @throws \yii\db\Exception
     */
    public function findByUserId($userid): \yii\db\DataReader|array
    {
        $sql = "select c.* from sys_user a inner join sys_user_role_rela b on a.userid = b.userid
            inner join sys_role c on b.roleid=c.roleid 
            where a.is_delete=0 and b.is_delete=0 and c.is_delete=0 and b.userid = :userid";

        return Yii::$app->db->createCommand($sql)->bindValue("userid",$userid)->queryAll();
    }
}
